//获取应用实例
var app = getApp();
var util = require('../../utils/util');
// var wxPromisify = require('../../utils/wxPromisify.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({
  data: {
    platform: '',
    totalCount: '',
    queryParams: {
      'pageNo': 0,
      'pageSize': 10,
    },
    items: [
      {
        coverUrl:'../../images/img3.png',
        title:'设备名称：某某奥克斯空调',
        lessonDesc:'位置：某某楼八楼会议室',
        price:'型号：V2655',
      }
    ],
    title: '',
  },

  onLoad: function () {
    // this.loadData();
    var IOS = wx.getStorageSync("IOS");

  },
  onShow: function () {
    // this.loadData();
  },
  //加载分页数据
  loadData: function () {
    var token = wx.getStorageSync('WXAPP_TOKEN');
    if (!token) {
      this.setData({
        title: '没有更多数据'
      });
      return;
    }
    this.data.queryParams['pageNo'] = this.data.queryParams['pageNo'] + 1;
    this.setData(this.data);
    var that = this;
    that.setData({
      title: "正在加载数据"
    });

    //获取列表
    wx.request({
      url: applet_park_api_url + '/daka/collectedLessons?token=' + token,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: that.data.queryParams,
      success: function (res) {
        if (res.data.success) {
          var hasData = false;
          if (res.data.data.page.rows.length > 0) {
            for (var row of res.data.data.page.rows) {
              that.data.items.push(row);
            }
          }
          that.data.totalCount = res.data.data.page.recordCount;
          that.setData(that.data);
          if (!hasData) {
            that.setData({
              title: "没有更多数据"
            });
          } else {
            that.setData({
              title: ""
            });
          }
          that.setData(that.data);
        } else {
          util.showModal('服务器开小差了,请稍后再试');
          that.setData({
            title: ""
          });
        }
      },
      fail: function (res) {
        util.showModal('服务器开小差了,请稍后再试');
        that.setData({
          title: ""
        });
      }
    })
  },
  // 详情
  topicDetail: function (e) {
    wx.navigateTo({
      url: '../discuss/discuss?id=' + e.currentTarget.dataset.id + '&title=' + e.currentTarget.dataset.title + '&lessonDesc=' + e.currentTarget.dataset.desc,
    });
  },
  //下拉
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.refresh();
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 2000
    })
  },
  //上拉
  onReachBottom: function () {
    
    this.setData({
      title: '正在加载数据'
    });
    this.loadData();
  },
  refresh: function () {
    this.setData({
      items: []
    });
    this.data.queryParams['pageNo'] = 0;
    this.loadData();
  },
  //收藏
  onCollectionTap: function (e) {
    var self = this;
    var index = e.currentTarget.dataset.index;
    var tradeId = e.currentTarget.dataset.tradeid;
    var collectTag = e.currentTarget.dataset.collecttag;
    var openid = wx.getStorageSync("OPENID");
    var url = '';
    if (collectTag == 0) {
      url = wx4wuxi_api_url + '/vehicle/collect'
    }
    if (collectTag == 1) {
      url = wx4wuxi_api_url + '/vehicle/cancelCollect'
    }
    wx.request({
      url: url,
      data: {
        openid: openid,
        tradeId: tradeId,
        appId: app.globalData.appid
      },
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: function (res) {
        if (res.data.success) {
          if (collectTag == 1) {
            if (self.data.items[index].tradeId == tradeId) {
              collectTag = 0;
              wx.showToast({
                title: '取消收藏',
                icon: 'success',
                duration: 2000
              })
            }
          } else {
            if (self.data.items[index].tradeId == tradeId) {
              collectTag = 1;
              wx.showToast({
                title: '收藏成功',
                icon: 'success',
                duration: 2000
              })
            }
          }
          self.data.items[index].collectTag = collectTag;
          self.setData(self.data);
        }
      }
    })
  },
  gotodetail: function (e) {
    
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../minicourse/minicourse?id=' + id,
    });
  },
});